<?php

namespace App\Http\Controllers\Api\CustomerOwn;

use Illuminate\Http\Request;
use App\Jobs\OrderSubmit;
use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Carbon\Carbon;
use App\Repository\UpThree\MainOrderRepostition;
use App\Http\Controllers\Api\BaseController;

class ZhongHengController extends BaseController
{
  protected $log = null;
  public function __construct()
  {
    $this->log = new Logger('ZhongHengPush');
    $this->log->pushHandler(
      (new RotatingFileHandler(storage_path('/logs/ZhongHengPush.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
    );
  }
  // 电信那边推送过来的数据 
  public function pushOrder(Request $Request)
  {
    // 刘志恒 让取消发货等状态接受
    // return json_encode(['responseCode' => 0, 'result' => '接受成功']);
    $getData = file_get_contents('php://input');
    $this->log->info('input' . ($getData));
    // $getData = '{"mid":2761,"order_number":"HK_SN_202303071519129792","trade_no":"HK_SN_202303071519129792","goods_id":306,"select_phone":"18652240974","express_name":"","express_no":"","create_time":1678173553,"status":2,"settle_status":1,"apply_fail_reason":"","first_recharge_money":"0.00","activate_time":0}';
    // return json_encode(['code' => 0, 'success' => true]);
    // $getData = '{
    //   "orderNum": "xxx",
    //   "orderStatus": "3",
    //   "handlerStatus": "7",
    //   "msg": "错误消息",
    //   "productCode": "dfasfasfsa",
    //   "outsideOrderNum": "HK_20230629154605612046503571",
    //   "activeTime": "2022-08-24 13:47:56",
    //   "trackeNum": "SF1231115545",
    //   "soldMobile": "152xxxxxxxx",
    //   "infoOrderNum": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
    //   "handlerUrl": "链接地址",
    //   "expressCompany": "xxxxxx",
    //   "correct": "0"
    // }';
    // 数据处理  
    if (empty($getData)) {
      return json_encode(['code' => 103, 'success' => false]);
    }
    if (!empty($getData) && is_string($getData)) {
      $getData = json_decode($getData, true);
    }
    if (empty($getData)) {
      return json_encode(['code' => 103, 'result' => false]);
    }
    // var_dump($getData,3333333333);
    $statusDatas = [
      0 => '提交中',
      1 => '提交上游',
      // 2 => '拒收单',
      10 => '审核中',
      11 => '待发货',
      20 => '已发货',
      30 => '已签收',
      40 => '已激活',
      50 => '异常单',
      51 => '取消单',
      60 => '失败单'
    ];
    // 100：初始状态,200：待支付,201：支付中,202：支付成功,203：支付失败,300：待订购,301：订购中,302：订购成功,303：订购失败,304：订购待重试,1000：待商户确认,1005：预处理成功,1010：预处理失败,1015：备货中,1020：已发货,1025：订购完成,1030：订购失败,1035：已激活,1040：激活失败，20：异步收单下单失败，其中302、303、1030、1035、20是订购终态
    $returnStatusArr = array(
      1 => 11,
      2 => 60,
      3 => 20,
      4 => 30,
      5 => 50,
      6 => 50,
      7 => 40,
      8 => 10,
      9 => 60,
      10 => 10,
      11 => 10,
      12 => 10,
      13 => 60,
    );
    $editData = [
      'id' => $getData['orderNum'],
      'sup_orderno' => $getData['orderNum'],
      'order_desc' => $statusDatas[$returnStatusArr[$getData['handlerStatus']]],
      'order_num' => $getData['orderNum'],
      // 'pagename' => $getData['订购产品']['产品名称'],
      'express_company' => !empty($getData['expressCompany']) ? $getData['expressCompany'] : '',
      'express_number' => !empty($getData['trackeNum']) ? $getData['trackeNum'] : '',
      'status' => !empty($returnStatusArr[$getData['handlerStatus']]) ? $returnStatusArr[$getData['handlerStatus']] : '',
      'iccid' => !empty($getData['soldMobile']) ? $getData['soldMobile'] : '',
      'three_msg' => !empty($getData['msg']) ? $getData['msg'] : '',
      'activat_at' => !empty($getData['activeTime']) ? $getData['activeTime'] : ''
    ];
    if (!empty($getData['trackeNum'])) {
      $data['status'] = 20;
    }

    $where = ['sup_orderno' => $getData['orderNum']];
    $model = new MainOrderRepostition();
    $res = $model->followOrderStatus($where, $editData);
    // var_dump($res, 1111111);
    $this->log->info('通知参数' . json_encode($getData) . '返回参数' . json_encode($res));
    if (!empty($res['code']) && $res['code'] == 200) {
      return json_encode(['responseCode' => 0, 'result' => '接受成功']);
      return 'success';
    }
    return json_encode(['responseCode' => 103, 'result' => '操作失败']);
    return 'error';
  }
}
